//
//  CBluetoothManager.h
//  testBluetooth2
//
//  Created by idiot.lin on 15/1/14.
//  Copyright (c) 2015年 ZKTeco Inc. All rights reserved.
//

#ifndef __testBluetooth2__CBluetoothManager__
#define __testBluetooth2__CBluetoothManager__
#import "objc2CplusMaroc.h"
#import <stdio.h>
#import <string>
#import <vector>

OBJC_CLASS(CBCentralManager);
OBJC_CLASS(CBPeripheral);
OBJC_CLASS(NSDictionary);
OBJC_CLASS(NSNumber);
OBJC_CLASS(NSError);
OBJC_CLASS(NSMutableArray );

using namespace std;

//<----blueTooth cmd list
#define  ZK_DEPRECATED(_version) __attribute__((deprecated))


//----->

typedef  enum
{
    ConnectSucess = 0,
    ConnectFailed = 1,
    FindServiceFailed = 2,
    FindchareterFailed = 3,
    ConnectUnkown = 4,
    DisConnect = 5
}opeRet;

typedef struct
{
    string serviceUUID;
    string strCharactersticsWriteUUID0;
    string strChracterCmdUUID;
}
PeriUuidInfo;


#define kNotificationBlueToothStateChange  @"NotificationBlueToothStateChange"
#define kNotificationBlueToothLiveState    @"NotificationBlueToothLiveState"
#define DICKeyBlueToothState               @"BlueToothState"
#define DICKeyBtOpeState                   @"state"
//add

typedef void(*discoverPeripheral)(CBCentralManager * central ,CBPeripheral *peripheral ,NSDictionary * advertisementData, NSNumber* RSSI);
typedef void(^handleDiscoverPeripheral)(CBCentralManager * central ,CBPeripheral *peripheral ,NSDictionary * advertisementData, NSNumber *RSSI);
typedef void(^handleDidDisconnectPeripheral)(CBPeripheral * peripheral,NSError *error);
typedef void(^competion)(NSError *error);

class CBluetoothManager
{
    public :
    static CBluetoothManager *instance();
    ~CBluetoothManager();
    bool disconnect();
    int btDeviceState();

    //数据通道提供给pullsdk使用的
    int write(char *buf, int offset, int count);
    int read(char *buf, int offset, int count);

    //修改蓝牙名称、蓝牙配对密码等
    int writeCmd(char *buf, int offset, int count);
    int readCmd(char *buf,int offset,int count);

    bool connect(char *cbPerKey,unsigned int port,int speed);

    //---从历史里面连接蓝牙设备，获得曾经连接过的对象，再传入进行连接
    bool connect(CBPeripheral *peripheral);
    CBCentralManager *getCenterManager();

    /**
     *  新增接口，针对配置搜索uuid
     *
     *  @param std::vector<PeriUuidInfo>perList std::vector<PeriUuidInfo>perList description
     *  @param scanByuuid                       scanByuuid description
     */
    void configureBlueTooth(std::vector<PeriUuidInfo>perList,bool scanByuuid);

    int searchBlueDevice(NSMutableArray *aryDevice,int timeOut = 4);
    void asyncSearchBlueDevice(NSMutableArray *aryDevice,handleDiscoverPeripheral findPeripheral,competion compet,int timeOut = 4);

    void setHandleDisconnect(handleDidDisconnectPeripheral disconnectHandle);

    
    void logBlueTooth();
    
    void clearBuffer();
    vector<PeriUuidInfo> perUuidInfo();
    bool scanByUUid();
    vector<PeriUuidInfo>perUuidList;
    private :
    
    bool _scanByUUid;
    CBluetoothManager();
    CBCentralManager * cbCenterManager;
    void * connectManager;
    CBPeripheral * bprContrl;
    bool waitBtPrepared();

};

#endif /* defined(__testBluetooth2__CBluetoothManager__) */











